package BarCode;

import org.apache.commons.lang3.StringUtils;

public class BarCodeTransformer {

    //true: postCode, false: barCode
    public boolean stringShouldBeWhichCode(String s) {
        if (StringUtils.isBlank(s)
                || (!isPostCodeChar(s) && !isBarCodeChar(s))) {
            throw new RuntimeException("Error input string.");
        }

        if (isPostCodeChar(s) && PostCode.isValidPostCode(s)) {
            return true;
        }

        if (isBarCodeChar(s) && BarCode.isValidBarCode(s)) {
            return false;
        }

        throw new RuntimeException("Error input!");
    }

    private boolean isPostCodeChar(String s) {
        return StringUtils.isNumeric(
                s.contains("-") ? s.replace("-","") : s);
    }

    private boolean isBarCodeChar(String s) {
        return s.contains("|") || s.contains(":");
    }

    public String transformer(String s) {
        if (stringShouldBeWhichCode(s)) {
            return PostCode.toBarCode(s);
        } else {
            return BarCode.toPostCode(s);
        }
    }
}
